package net.xdclass.strategy;

import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


/**
 * 功能描述
 *
 * @author ASUS
 * @date 2024/07/27  23:12
 */
public class ShardingDBConfig {
    /**
     * 存储数据库位置编号
     */
    private static final List<String> dbPrefixList = new ArrayList<>();

    private static Random random = new Random();

    //配置启用那些库的前缀
    static{
        dbPrefixList.add("0");
        dbPrefixList.add("1");
        dbPrefixList.add("a");
    }

    /**
     * 获取随机的前缀
     * @return
     */
    public static String getRandomDBPrefix(String code){
        int hashCode = code.hashCode();
        int index = Math.abs(hashCode) % dbPrefixList.size();
       //随机
//        int index = random.nextInt(dbPrefixList.size());
        return dbPrefixList.get(index);
    }

}
